import FooterView from '@/components/FooterView.vue'
import MyPageView from '@/components/MyPageView.vue'
export default {
    name: "pShow",
    data() {
        return {
            jingXuanList: [],
            currentPageData: [],
            userLike: localStorage.getItem('userLike'),
            tempType: '',
            isSelect: false,
            guessLike: [],
            selectList: [],
            isSearch: false,
            inputVal: '',
            products: [],
            p: [],
            type: ['temp', '小说', '艺术', '历史', '法律', '心理', '儿童文学', '动漫绘本',
                '启蒙益智', '技能培养', '中小教辅', '字典词典', '时尚娱乐', '养生保健', '管理', '经济'],
            activeIndex: '1',
            pageSize: 100,
            curpage: 1,
            allcount: 0,
            searchResult: '',
            isShowSearch: false
        };
    },
    created() {
        this.axios.defaults.headers.common["Authorization"] = localStorage.getItem("token");
        this.axios.get("api/getAllBooks").then(res => {
            let randomNumber = this.getRandomInt(10, 880);
            let end = randomNumber + 12
            this.products = res.data.data;
            this.jingXuanList = this.products.slice(randomNumber, end)
            this.guessLike = this.products.filter((item) => {
                return item.type == this.type[this.userLike]
            })
            if (this.guessLike.length > 12) {
                this.guessLike = this.guessLike.slice(0, 12)
            }
        });
    },
    components: { FooterView, MyPageView },
    methods: {
        // 生成指定范围内的随机整数
        getRandomInt(min, max) {
            // Math.floor()向下取整，Math.random()生成[0,1)之间的随机数
            // 这里的max和min是包括在内的，所以要写成(max - min + 1)
            return Math.floor(Math.random() * (max - min + 1) + min);
        },
        handleCurrentChange(val) {
            let pageSize = 12 // 每页显示的数量
            let currentPage = 1 // 当前页码
            let totalData = this.selectList // 全部图书数据
            let totalPages = Math.ceil(totalData.length / pageSize) // 总共的页码数
            // 分页计算
            let start = (currentPage - 1) * pageSize
            let end = start + pageSize
            // 页码选择器
            if (val >= 1 && val <= totalPages) {
                currentPage = val
                start = (currentPage - 1) * pageSize
                end = start + pageSize
                this.currentPageData = totalData.slice(start, end)
            }
        },
        goDetail(pId) {
            this.$router.push("/index/pDetail?productId=" + pId);
        },
        handleSelect(key, keyPath) {
            console.log('123', key, keyPath);
        },
        pageChange(a) {
            console.log('当前页', a);
            this.curpage = a
        },
        open2() {
            this.$notify({
                message: '请输入搜索内容',
                type: 'error',
                showClose: false,
                duration: 1000,
                offset: 10
            });
        },
        search() {
            this.isShowSearch = true
            if (this.inputVal == '') {
                this.open2()
            } else {
                let product1 = new RegExp(this.inputVal, 'g')
                let p = this.products
                if (p) {
                    p = p.filter((item) => {
                        return product1.test(item.productName)
                    })
                }
                this.searchResult = p
            }
        },
        classify(val) {
            this.inputVal = ''
            this.isShowSearch = false
            this.searchResult = ''
            this.tempType = this.type[val]
            if (val < 3) {
                this.tempType = '文学艺术' + '/' + this.tempType
            } else if (val < 6) {
                this.tempType = '人文社科' + '/' + this.tempType
            } else if (val < 10) {
                this.tempType = '少儿童书' + '/' + this.tempType
            } else if (val < 12) {
                this.tempType = '教育考试' + '/' + this.tempType
            } else if (val < 14) {
                this.tempType = '生活休闲' + '/' + this.tempType
            } else {
                this.tempType = '经济金融' + '/' + this.tempType
            }
            this.selectList = this.products.filter((item) => {
                return item.type == this.type[val]
            })
            this.currentPageData = this.selectList.slice(0, 12)
        }
    },

}